#!/bin/bash
#
# @name:    get_latest_releases
# @repo:    https://gitee.com/TangYuanMaster/JiuWei
# @author:  TangYuanMaster
# @license: See the file 'LICENSE.txt'
#

set -e

# 初始化颜色变量
COLOR_DEFAULT="\e[0m"
COLOR_RED="\e[31m"
COLOR_GREEN="\e[32m"
COLOR_YELLOW="\e[33m"
COLOR_BLUE="\e[34m"
COLOR_PURPLE="\e[35m"
COLOR_CYAN="\e[36m"

interrupt_handler() {
    echo -e "${COLOR_YELLOW}[*] Received Ctrl+C, Do you want to continue running the script? (Y/n)${COLOR_DEFAULT}"
    read -r answer
    case $answer in
        [Nn]) exit 1;;
        *) continue;;
    esac
}
trap interrupt_handler SIGINT

if [ $# -lt 2 ]; then
    echo -e "${COLOR_YELLOW}[*] Please provide the target Github project address and target file name.${COLOR_DEFAULT}"
    exit 1
fi

MAIN_DIR="$HOME/JiuWei"
INED_FILE=".installed" #记录所有已安装的包名（文本文件）
PACKAGES_INFO=".packages_info" #记录所有的包信息,方便search（文本文件）
PACKAGES_NAME=".packages_name" #记录所有的包名称,方便install（文本文件）
CONFIG_FILE="config.list" #配置文件（文本文件）
FOXINDEX_FILE=".foxindex" #放置FOXINDEX解压后产生的文件（文件夹）
CACHE_FILE=".cache" #放置从源中下载的安装包（文件夹）
BIN_FILE=".bin" #快捷命令存放（文件夹）
ST_FILE=".status" #JiuWei状态检测（文本文件）
DEFAULT_ANSWER=Y

INED="$MAIN_DIR/$INED_FILE"
PACKAGES_INFO="$MAIN_DIR/$PACKAGES_INFO"
PACKAGES_NAME="$MAIN_DIR/$PACKAGES_NAME"
CACHE="$MAIN_DIR/$CACHE_FILE"
FOXINDEX="$MAIN_DIR/$FOXINDEX_FILE"
BIN="$MAIN_DIR/$BIN_FILE"
ST="$MAIN_DIR/$ST_FILE"

#get_latest_releases -z "项目地址" "架构关键词(用于下载对应架构文件)" "解压到的文件夹"
#get_latest_releases -tg "项目地址" "架构关键词(用于下载对应架构文件)" "解压到的文件夹"
#get_latest_releases -w "项目地址" "文件关键词(用于下载对应文件)" "下载到的文件夹"

command=$1
github_repo=$2
keyword=$3
output_filename=$4

case "$command" in
    -z)
        latest_release=$(curl -sL "https://api.github.com/repos/$repo/releases/latest")
        download_urls=$(echo "$latest_release" | grep -oE '"browser_download_url": "[^"]+"' | cut -d'"' -f4)
        for url in $download_urls; do
            if [[ $url == *"$keyword"* ]]; then
                target_file=${url##*/}
                echo -e "${COLOR_GREEN}[+] Download $target_file. ${COLOR_DEFAULT}"
                wget "$url" -O "$CACHE/$target_file"
            fi
        done
        mkdir "$MAIN_DIR/$output_filename"
        unzip -o -d "$MAIN_DIR/$output_filename" "$CACHE/$target_file"
        echo -e "#!/bin/bash\n(cd $MAIN_DIR/$output_filename && ./*$keyword* $@)" > $BIN/$output_filename && chmod +x $BIN/$output_filename
        echo -e "${COLOR_GREEN}[+] Save to $MAIN_DIR/$output_filename/ . ${COLOR_DEFAULT}"
        ;;
    -tg)
        latest_release=$(curl -sL "https://api.github.com/repos/$repo/releases/latest")
        download_urls=$(echo "$latest_release" | grep -oE '"browser_download_url": "[^"]+"' | cut -d'"' -f4)
        for url in $download_urls; do
            if [[ $url == *"$keyword"* ]]; then
                target_file=${url##*/}
                echo -e "${COLOR_GREEN}[+] Download $target_file. ${COLOR_DEFAULT}"
                wget "$url" -O "$CACHE/$target_file"
            fi
        done
        mkdir "$MAIN_DIR/$output_filename"
        tar -zxvf "$CACHE/$target_file" -C "$MAIN_DIR/$output_filename"
        echo -e "#!/bin/bash\n(cd $MAIN_DIR/$output_filename && ./*$keyword* $@)" > $BIN/$output_filename && chmod +x $BIN/$output_filename
        echo -e "${COLOR_GREEN}[+] Save to $MAIN_DIR/$output_filename/ . ${COLOR_DEFAULT}"
        ;;
    -w)
        latest_release=$(curl -sL "https://api.github.com/repos/$repo/releases/latest")
        download_urls=$(echo "$latest_release" | grep -oE '"browser_download_url": "[^"]+"' | cut -d'"' -f4)
        for url in $download_urls; do
            if [[ $url == *"$keyword"* ]]; then
                target_file=${url##*/}
                echo -e "${COLOR_GREEN}[+] Download $target_file. ${COLOR_DEFAULT}"
                mkdir "$MAIN_DIR/$output_filename"
                wget "$url" -O "$MAIN_DIR/$output_filename/$target_file"
                echo -e "#!/bin/bash\n(cd $MAIN_DIR/$output_filename && ./*$keyword* $@)" > $BIN/$output_filename && chmod +x $BIN/$output_filename
                echo -e "${COLOR_GREEN}[+] Save to $MAIN_DIR/$output_filename/ . ${COLOR_DEFAULT}"
            fi
        done
        ;;
    *)
        echo -e "${COLOR_YELLOW}[*] Unknown command: $command.${COLOR_DEFAULT}"
        ;;
esac
